// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Hash.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Using Directives

    using System;
    using System.Security.Cryptography;
    using System.Text;

    #endregion

    /// <summary>
    /// The hash.
    /// </summary>
    public class Hash
    {
        #region Constants and Fields

        /// <summary>
        /// The hash.
        /// </summary>
        private static readonly MD5 hash = new MD5CryptoServiceProvider();

        #endregion

        #region Public Methods

        /// <summary>
        /// The create hash key.
        /// </summary>
        /// <param name="s">
        /// The s.
        /// </param>
        /// <returns>
        /// </returns>
        public static Guid CreateHashKey(string s)
        {
            hash.Initialize();
            var inBytes = Encoding.UTF8.GetBytes(s);
            hash.ComputeHash(inBytes);
            var outBytes = hash.Hash;
            var outBytes2 = new byte[8];
            Array.ConstrainedCopy(outBytes, 3, outBytes2, 0, 8);

            var g = new Guid(outBytes[0] * outBytes[1], outBytes[1], outBytes[2], outBytes2);

            return g;
        }

        #endregion
    }
}